package com.baichuan.zntx.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baichuan.common.core.utils.MapstructUtils;
import com.baichuan.common.core.utils.StringUtils;
import com.baichuan.common.mybatis.core.page.TableDataInfo;
import com.baichuan.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baichuan.zntx.domain.bo.DeviceHistoryBo;
import com.baichuan.zntx.domain.vo.DeviceHistoryVo;
import com.baichuan.zntx.domain.DeviceHistory;
import com.baichuan.zntx.mapper.DeviceHistoryMapper;
import com.baichuan.zntx.service.IDeviceHistoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 历史状态Service业务层处理
 *
 * @author Baichuan
 * @date 2024-05-11
 */
@RequiredArgsConstructor
@Service
public class DeviceHistoryServiceImpl implements IDeviceHistoryService {

    private final DeviceHistoryMapper baseMapper;

    /**
     * 查询历史状态
     */
    @Override
    public DeviceHistoryVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询历史状态列表
     */
    @Override
    public TableDataInfo<DeviceHistoryVo> queryPageList(DeviceHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DeviceHistory> lqw = buildQueryWrapper(bo);
        Page<DeviceHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询历史状态列表
     */
    @Override
    public List<DeviceHistoryVo> queryList(DeviceHistoryBo bo) {
        LambdaQueryWrapper<DeviceHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DeviceHistory> buildQueryWrapper(DeviceHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DeviceHistory> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceId()), DeviceHistory::getDeviceId, bo.getDeviceId());
        lqw.eq(bo.getStatus() != null, DeviceHistory::getStatus, bo.getStatus());
        if(ObjectUtil.isNotNull(bo.getTimespan()) && bo.getTimespan().size()==2){
            lqw.between(DeviceHistory::getCreateTime, bo.getTimespan().get(0), bo.getTimespan().get(1));
        }
        lqw.orderByDesc(DeviceHistory::getCreateTime);
        return lqw;
    }

    /**
     * 新增历史状态
     */
    @Override
    public Boolean insertByBo(DeviceHistoryBo bo) {
        DeviceHistory add = MapstructUtils.convert(bo, DeviceHistory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改历史状态
     */
    @Override
    public Boolean updateByBo(DeviceHistoryBo bo) {
        DeviceHistory update = MapstructUtils.convert(bo, DeviceHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DeviceHistory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除历史状态
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
